package com.ruoyi.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzhj
 * @version 1.0.0
 * @className Test001.java
 * @description
 * @date 2021年01月26日 21:27:00
 */
public class ReadJsonUtil {

    private static String filePath = "E:\\工作\\questionMS\\ABC.json";

    /**
     * 获取json
     *
     * @return
     */
    public static String getJson() {
        String jsonStr = "";
        try {
            File file = new File(filePath);
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            return null;
        }
    }

    // 读取文件内容
    public static String readFile() {//路径
        File file = new File(filePath);
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    // 读取文件内容
    public static List<String> readFileList() {//路径
        List<String> list = new ArrayList<>();
        File file = new File(filePath);
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                list.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static void main(String[] args) {
        List<String> list = readFileList();
        JSONArray jsonArray = new JSONArray();
        for (String s : list) {
            Object parse = JSONObject.parse(s);
            jsonArray.add(parse);
        }
        List<SubjectEntity> subjectEntities1 = jsonArray.toJavaList(SubjectEntity.class);
        System.out.println(subjectEntities1);
    }

//    public static Map<String, String> subjetTypeMap = new HashMap(){{
//        put("01","1");//单选
//        put("02","2");//多选
//        put("03","3");//判断
//    }};

}
